<template>
  <div class="addxd">
    <dnav :data="['消毒管理', '添加消毒']" />
    <el-select @change="sel" v-model="ass_id" placeholder="请选择未完成任务">
      <el-option
        v-for="v in notwriteass"
        :key="v.ass_id"
        :label="v.aname"
        :value="v.ass_id"
      >
        <span style="float: left">{{ v.aname }}</span>
        <span style="float: right; color: #8492a6; font-size: 13px">{{
          v.to_time
        }}</span>
      </el-option>
    </el-select>
    <el-date-picker
      type="date"
      style="width: 300px"
      placeholder="选择日期"
      v-model="over_time"
      value-format="yyyy-MM-dd"
    ></el-date-picker>

    <el-card v-if="ass_id && over_time">
      <h4>记录消毒结果</h4>
      <br />
      <div class="mitw">
        <el-form
          :label-position="labelPosition"
          label-width="90px"
          v-for="v in xdform"
          :key="v.wu_id"
        >
          <el-form-item :label="v.wuname">
            <el-input
              :placeholder="v.wuname"
              v-model="v.remark"
              style="margin: 5px 0"
            ></el-input>
          </el-form-item>
        </el-form>
        <el-button type="success" @click="addw()">添加</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      notwriteass: [], //未完成任务
      ass_id: "", //任务ID
      over_time: "", //完成时间
      xdform: [], //消毒表单
      labelPosition: "right", //输入框位置
    };
  },
  methods: {
    getnotass() {
      //获取未完成任务
      let obj = {
        man_id: JSON.parse(sessionStorage.getItem("userinfo")).man_id,
        asst_id: 2,
      };
      this.$api.NotWriteAss(obj).then((res) => {
        console.log(res);
        if (res.data.code == 200) {
          this.notwriteass = res.data.data;
        }
      });
    },
    getmit() {
      //获取浮游生物
      this.$api.Getmit().then((res) => {
        console.log(res);
        if (res.data.code == 200) {
          this.xdforms = res.data.data;
        }
      });
    },
    getxdobj() {
      //获取消毒对象
      this.$api.Getxdobj().then((res) => {
        console.log(res);
        if (res.data.code == 200) {
          let arr = res.data.data;
          this.xdform = arr.map((v) => {
            v.remark = "";
            return v;
          });
        }
      });
    },
    sel() {
      this.getxdobj();
    },
    addw() {
      //判断是否为空
      let t = this.xdform.some((v) => v.remark == "");
      if (t) {
        this.$message({
          type: "info",
          message: "不能为空",
        });
        return;
      }
      //组合参数
      let { man_id } = JSON.parse(sessionStorage.getItem("userinfo"));
      let asst_id = this.asst_id;
      let ass_id = this.ass_id;
      let over_time = this.over_time;
      let room_id = this.notwriteass.find((v) => v.ass_id == ass_id).room_id;
      console.log(room_id);
      let params = {
        man_id,
        ass_id,
        asst_id,
        over_time,
      };
      //记录值
      let data = [];
      data = this.xdform.map((v) => {
        let obj = {
          room_id,
          ass_id,
          wu_id: v.wu_id,
          remark: v.remark,
        };
        return obj;
      });
      data = JSON.stringify(data);
      params.data = data;
      this.$api.Addxd(params).then((res) => {
        console.log(res);
        if (res.data.code == 200) {
          this.getnotass();
          this.getmit();
          this.$store.dispatch("getnotass");
        }
      });
    },
  },
  mounted() {
    this.getnotass();
    this.getmit();
  },
};
</script>

<style>
</style>